package CCRMClient.GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.CardLayout;
import java.awt.Color;

import javax.swing.border.LineBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import CCRMClient.ClientStart;
import CCRMClient.Controllers.LoginMng;
import Common.SERVER_COMMANDS;
import Common.Entities.Employee;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
/**
 * This class implements the CCRM Client GUI window and the log-in screen
 * @author Ori Ziv
 *
 */
/**
 * @author lock
 *
 */
public class MainFrame extends JFrame implements ResponseToScreen {

	private JPanel contentPane;
	private JPanel Login;
	private JLabel lblCCRMClient;
	private JLabel lblMandatoryFields;
	private JLabel lblUserName;
	private JLabel lblPassword;
	private JLabel lblServerName;
	private JLabel lblPort;
	private JTextField txtUserName;
	private JPasswordField pswdPassword;
	private JTextField txtServerName;
	private JTextField txtPort;
	private JButton btnLogin;
	private JButton btnCancel;

	/*
	 * Launch the application.
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
*/
	/**
	 * Create the frame.
	 */
	public MainFrame() {
		initialize();
	}
	/**
	 * This method initialize the frame
	 * @author Ori Ziv
	 */
	public void initialize() {
		setTitle("CCRM Client - Group 3");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 380, 364);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new LineBorder(new Color(0, 0, 0)));
		setContentPane(contentPane);
		contentPane.setLayout(new CardLayout(0, 0));
		contentPane.add(getLogin(), "name_523132592433810");
	}
	/**
	 * This method initialize the login panel
	 * @return JPanel - initialized login panel
	 * @author Ori Ziv
	 */
	public JPanel getLogin() {
		if (Login == null) {
			Login = new JPanel();
			Login.setSize(380, 400);
			Login.setBackground(Color.WHITE);
			Login.setBorder(new LineBorder(new Color(0, 0, 0)));
			Login.setLayout(null);
			Login.add(getLblCCRMClient());
			Login.add(getLblMandatoryFields());
			Login.add(getLblUserName());
			Login.add(getLblPassword());
			Login.add(getLblServerName());
			Login.add(getLblPort());
			Login.add(getTxtUserName());
			Login.add(getPswdPassword());
			Login.add(getTxtServerName());
			Login.add(getTxtPort());
			Login.add(getBtnLogin());
			Login.add(getBtnCancel());
		}
		return Login;
	}
	
	/**
	 * This method initialize and return lblCCRMClient label
	 * @return 
	 */
	private JLabel getLblCCRMClient() {
		if (lblCCRMClient == null) {
			lblCCRMClient = new JLabel("CCRM Client");
			lblCCRMClient.setHorizontalAlignment(SwingConstants.CENTER);
			lblCCRMClient.setForeground(Color.RED);
			lblCCRMClient.setFont(new Font("David", Font.BOLD, 26));
			lblCCRMClient.setBackground(Color.WHITE);
			lblCCRMClient.setBounds(10, 11, 336, 49);
		}
		return lblCCRMClient;
	}
	
	/**
	 * This method initialize and return lblMandatoryFields label
	 * @return
	 */
	private JLabel getLblMandatoryFields() {
		if (lblMandatoryFields == null) {
			lblMandatoryFields = new JLabel("All fields are mandatory!");
			lblMandatoryFields.setForeground(Color.RED);
			lblMandatoryFields.setFont(new Font("David", Font.BOLD, 12));
			lblMandatoryFields.setBounds(22, 71, 175, 14);
		}
		return lblMandatoryFields;
	}
	
	/**
	 * This method initialize and return lblUserName label
	 * @return
	 */
	private JLabel getLblUserName() {
		if (lblUserName == null) {
			lblUserName = new JLabel("User Name:");
			lblUserName.setFont(new Font("David", Font.BOLD, 20));
			lblUserName.setBounds(22, 110, 117, 29);
		}
		return lblUserName;
	}
	
	/**
	 * This method initialize and return lblPassword label
	 * @return
	 */
	private JLabel getLblPassword() {
		if (lblPassword == null) {
			lblPassword = new JLabel("Password:");
			lblPassword.setFont(new Font("David", Font.BOLD, 20));
			lblPassword.setBounds(22, 150, 117, 29);
		}
		return lblPassword;
	}
	
	/**
	 * This method initialize and return lblServerName label
	 * @return
	 */
	private JLabel getLblServerName() {
		if (lblServerName == null) {
			lblServerName = new JLabel("Server:");
			lblServerName.setFont(new Font("David", Font.BOLD, 20));
			lblServerName.setBounds(22, 200, 117, 29);
		}
		return lblServerName;
	}
	
	/**
	 * This method initialize and return lblPort label
	 * @return
	 */
	private JLabel getLblPort() {
		if (lblPort == null) {
			lblPort = new JLabel("Port:");
			lblPort.setFont(new Font("David", Font.BOLD, 20));
			lblPort.setBounds(22, 245, 117, 29);
		}
		return lblPort;
	}
	
	/**
	 * This method initialize and return txtUserName text field
	 * @return
	 */
	private JTextField getTxtUserName() {
		if (txtUserName == null) {
			txtUserName = new JTextField();
			txtUserName.setColumns(10);
			txtUserName.setBounds(178, 110, 147, 20);
		}
		return txtUserName;
	}
	
	/**
	 * This method initialize and return pswdPassword password field
	 * @return
	 */
	private JPasswordField getPswdPassword() {
		if (pswdPassword == null) {
			pswdPassword = new JPasswordField();
			pswdPassword.setBounds(178, 151, 147, 20);
		}
		return pswdPassword;
	}
	
	/**
	 * This method initialize and return txtServerName text field
	 * @return
	 */
	private JTextField getTxtServerName() {
		if (txtServerName == null) {
			txtServerName = new JTextField();
			txtServerName.setColumns(10);
			txtServerName.setBounds(178, 201, 147, 20);
		}
		return txtServerName;
	}
	
	/**
	 * This method initialize and return txtPort text field
	 * @return
	 */
	private JTextField getTxtPort() {
		if (txtPort == null) {
			txtPort = new JTextField();
			txtPort.setText("11170");
			txtPort.setColumns(10);
			txtPort.setBounds(178, 246, 147, 20);
		}
		return txtPort;
	}
	
	/**
	 * This method initialize and return login button
	 * @return
	 */
	private JButton getBtnLogin() {
		if (btnLogin == null) {
			btnLogin = new JButton("Log-in");
			btnLogin.addActionListener(new BtnLoginActionListener());
			btnLogin.setFont(new Font("David", Font.BOLD, 12));
			btnLogin.setBounds(58, 286, 89, 23);
		}
		return btnLogin;
	}
	
	/**
	 * This method initialize and return cancel button
	 * @return
	 */
	private JButton getBtnCancel() {
		if (btnCancel == null) {
			btnCancel = new JButton("Cancel");
			btnCancel.addActionListener(new BtnCancelActionListener());
			btnCancel.setFont(new Font("David", Font.BOLD, 12));
			btnCancel.setBounds(217, 285, 89, 23);
		}
		return btnCancel;
	}
	
	public void notifyUser(String message, String title, String type) {
		switch (type) {
		case "ERROR_MESSAGE":
			JOptionPane.showMessageDialog(this, message, title, JOptionPane.ERROR_MESSAGE);
			break;
		case "WARNING_MESSAGE":
			JOptionPane.showMessageDialog(this, message, title, JOptionPane.WARNING_MESSAGE);
			break;
		case "INFORMATION_MESSAGE":
			JOptionPane.showMessageDialog(this, message, title, JOptionPane.INFORMATION_MESSAGE);
			break;
		default:
			JOptionPane.showMessageDialog(this, message, title, JOptionPane.PLAIN_MESSAGE);
			break;
		}
	}
	
	/* (non-Javadoc)
	 * @see CCRMClient.GUI.ResponseToScreen#handleResponseFromClient(java.lang.Object[])
	 */
	@Override
	public void handleResponseFromClient(Object... objects) {
		if (objects[0] instanceof String) {
			JOptionPane.showMessageDialog(this, (String)objects[0], "Login problem", JOptionPane.INFORMATION_MESSAGE);
			txtUserName.setText("");
			pswdPassword.setText("");
			txtServerName.setText("");
			txtPort.setText("11170");
			Login.setSize(380, 400);
			ClientStart.panelSwicher(getLogin());
		} else {
			EmployeePortal empPortal = new EmployeePortal();
			this.getContentPane().add(empPortal, "EmployeePortal");
			Login.setVisible(false);
			this.setSize(empPortal.getWidth(), empPortal.getHeight());
			empPortal.setVisible(true);
		}
	}
	
	/**
	 * This inner class implements event listener for pressing Cancel button
	 * @author Ori Ziv
	 *
	 */
	private class BtnCancelActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			if (ClientStart.client != null && ClientStart.client.isConnected()) {
				try {
					ClientStart.client.closeConnection();
				} catch (IOException e1) {
					JOptionPane.showMessageDialog(null, e1.getMessage(), "Communication Error", JOptionPane.ERROR_MESSAGE);
				} finally {
					dispose();
					System.exit(0);
				} // end of try - catch finally clause
			} // end of if
			dispose();
			System.exit(0);
		} // end of method actionPerformed
	} // end of inner class BtnCancelActionListener
	
	/**
	 * This inner class implements event listener for pressing Login button
	 * @author Ori Ziv
	 *
	 */
	private class BtnLoginActionListener implements ActionListener {
		
		public void actionPerformed(ActionEvent e) {
			char[] rawPassword;
			String userName;
			String server;
			String port;
			
			try {
				userName = txtUserName.getText();
				rawPassword = pswdPassword.getPassword();
				server = txtServerName.getText();
				port = txtPort.getText();
				if (userName.isEmpty() || rawPassword.length == 0 || server.isEmpty() || port.isEmpty()) {
					JOptionPane.showMessageDialog(null, "All fields are mandatory!", "Login Error", JOptionPane.ERROR_MESSAGE);
					txtUserName.setText("");
					pswdPassword.setText("");
					txtServerName.setText("");
					txtPort.setText("11170");
				} else {
					try {
						ClientStart.initCCRMClient(server, port);
						ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.LOG_IN, LoginMng.loginUser(userName, rawPassword));
					} catch (IOException e1) {
						JOptionPane.showMessageDialog(null, e1.getMessage(), "Communication Error", JOptionPane.ERROR_MESSAGE);
					} // end of IOException handler				
				} // end of if-else clause
			} catch ( NullPointerException e2) {
				JOptionPane.showMessageDialog(null, "All fields are mandatory!", "Login Error", JOptionPane.ERROR_MESSAGE);
			} // end of NullPointerException handler
		} // end of method actionPerformed
	} // end of inner class BtnLoginActionListener
}
